package cn.com.swu.mianyangapp.ui.notifications;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSON;
import com.google.android.material.tabs.TabLayout;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.com.swu.mianyangapp.R;
import cn.com.swu.mianyangapp.bean.Notice;
import cn.com.swu.mianyangapp.bean.Text;
import cn.com.swu.mianyangapp.databinding.FragmentNotificationsBinding;
import cn.com.swu.mianyangapp.ui.message.HttpUtil;
import cn.com.swu.mianyangapp.ui.message.RecvAdapter;
import cn.com.swu.mianyangapp.ui.message.ViewTextActivity;

public class NotificationsFragment extends Fragment {

    private String noticeurl="http://192.168.43.211:8081/";

    private FragmentNotificationsBinding binding;

    public ListView noticeListview;
    public List<Notice> noticeList =new ArrayList<Notice>();

    public ListCardAdapter noticeAdapter;



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        NotificationsViewModel notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        View view=inflater.inflate(R.layout.fragment_notifications,container,false);
        noticeListview = (ListView)view.findViewById(R.id.Notice_list);


        String url=noticeurl+"querryNotice";
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Response response) throws IOException {

                String responseData=response.body().string();
                changeWithJSONObject(responseData);
            }
        });
        noticeListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
            {
                ListView listView = (ListView) adapterView;
                Notice notice=(Notice) listView.getItemAtPosition(position);
                String noticeTitle=notice.getNoticeTitle();
                String noticeContent=notice.getNoticeContent();
                String publisherID=notice.getPublisherID().toString();
                String Clicks=notice.getClicks().toString();
                String StartTime=notice.getStartTime();


                String url=noticeurl+"showNotice"+"?noticeID="+notice.getNoticeID();
                HttpUtil.sendOkHttpRequest(url, new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        System.out.println(e);
                    }
                    @Override
                    public void onResponse(Response response) throws IOException {
                        String responseData=response.body().string();
                    }
                });
                // Toast.makeText(draftlist.get(position), content[position], Toast.LENGTH_LONG).show();
                // 第二步：通过Intent跳转至新的页面
                Intent intent = new Intent(getActivity(), ViewNoticeActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("mnoticeTitle", noticeTitle);
                bundle.putString("mnoticeContent",noticeContent);
                bundle.putString("mpublisherID",publisherID);
                bundle.putString("mClicks",Clicks);
                bundle.putString("mStartTime",StartTime);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });


        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
        String url=noticeurl+"querryNotice";
        HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                System.out.println(e);
            }

            @Override
            public void onResponse(Response response) throws IOException {

                String responseData=response.body().string();
                changeWithJSONObject(responseData);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    private void changeWithJSONObject(String jsonData) {
        //循环遍历取出json数组数据并存储到draftlist中
        noticeList= JSON.parseArray(jsonData, Notice.class);
        System.out.println(noticeList);
        Message msg=new Message();
        msg.what=1;
        handler.sendMessage(msg);
    }
    public Handler handler=new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    noticeAdapter = new ListCardAdapter(getActivity(), noticeList);
                    noticeListview.setAdapter(noticeAdapter);
                    break;
            }
        }

    };
}